package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 20:50 2021/1/5
 * @ModifiedBy:
 */
@RestController
@RequestMapping("movements")
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * @Description: 发送动态
     * @Param:[textContent, location, longitude, latitude, multipartFile, token]
     * @return:org.springframework.http.ResponseEntity<java.lang.Void>
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    @PostMapping
    public ResponseEntity<Void> savePublish(@RequestParam(value = "textContent") String textContent,
                                            @RequestParam(value = "location") String location,
                                            @RequestParam(value = "longitude") String longitude,
                                            @RequestParam(value = "latitude") String latitude,
                                            @RequestParam(value = "imageContent") MultipartFile[] multipartFile,
                                            @RequestHeader("Authorization") String token) {
        try {
            String publishId = this.movementsService.savePublish(textContent, location, longitude, latitude, multipartFile, token);
            if (StringUtils.isNotEmpty(publishId)) {
                //发送消息
                this.quanziMQService.queryPublishMsg(publishId);

                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 查询推荐的动态信息
     * @Param:[page, pageSize]
     * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.PageResult>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.movementsService.queryRecommendPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 查询好友的动态信息
     * @Param:[page, pageSize]
     * @return:com.tanhua.server.vo.PageResult
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.movementsService.queryUserPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * @Description: 点赞
     * @Param:[publishId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Long>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        try {
            Long count = this.movementsService.likeComment(publishId);
            if (null != count) {
                //点赞
                this.quanziMQService.likePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 取消点赞
     * @Param:[publishId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Long>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        try {
            Long count = this.movementsService.disLikeComment(publishId);
            if (null != count) {
                //发送消息
                this.quanziMQService.disLikePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 喜欢
     * @Param:[publishId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Long>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        try {
            Long count = this.movementsService.loveComment(publishId);
            if (null != count) {
                //发送消息
                this.quanziMQService.lovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 不喜欢
     * @Param:[publishId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Long>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unLoveComment(@PathVariable("id") String publishId) {
        try {
            Long count = this.movementsService.unLoveComment(publishId);
            if (null != count) {
                //发送消息
                this.quanziMQService.disLovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 查询单条信息
     * @Param:[publishId]
     * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.Movements>
     * @Author:JinboYang
     * @Date:2021/1/7
     */
    @GetMapping("{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("id") String publishId) {
        try {
            Movements movements = this.movementsService.queryMovementsById(publishId);
            if (null != movements) {
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 谁看过我
     * @Param:[]
     * @return:org.springframework.http.ResponseEntity<java.util.List<com.tanhua.server.vo.VisitorsVo>>
     * @Author:JinboYang
     * @Date:2021/1/13
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList() {
        try {
            List<VisitorsVo> list = this.movementsService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 自己所有的动态
       * @Param:[page, pageSize, userId]
       * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.PageResult>
       * @Author:JinboYang
       * @Date:2021/1/13
       */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.movementsService.queryAlbumList(page, pageSize, userId);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
}
